/*
 * @LastEditors: yuSeen
 * @LastEditTime: 2022-01-09 20:09:59
 */
import { IAllObject } from "@/types/base";

export const useSong = () => {
  /**
   * @description: 获取歌曲名称
   * @param {string} name //原始名称
   * @param {string} alia //附加名称列表
   * @return {*}  html字符串,需要使用v-html解析
   */
  const getSongName = (name: string, alia: string[]) => {
    try {
      const aliaName = alia.reduce((pre: string[], cur: string) => pre.concat(cur), []).join();
      if (aliaName) return `<span class="text-common-text-color">${name}</span> <span class="text-sub-text-color">(${aliaName})</span>`;
      return `<span class="text-common-text-color">${name}</span>`;
    } catch (error) {
      return "未知歌曲";
    }
  };

  // 获取歌曲歌手
  const getSingers = (ar: IAllObject[]) => {
    try {
      return ar.reduce((pre, cur) => pre.concat(cur.name), []).join(" / ");
    } catch (error) {
      return "未知艺术家";
    }
  };

  // 时间补零
  const setZero = (num: number) => {
    if (num === 0) return "00";
    if (num < 10) return "0" + num;
    return num;
  };

  // 计算歌曲时间
  const getSongTime = (ms: number) => {
    let m = setZero(Math.floor(ms / 60000));
    let s = setZero(Math.floor((ms / 1000) % 60));
    return m + ":" + s;
  };

  return {
    getSongName,
    getSingers,
    setZero,
    getSongTime,
  };
};
